package bb.lanxing.activity.bluetooth.radar;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.core.content.res.ResourcesCompat;
import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.databinding.DataBindingUtil;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.databinding.ActivityRadarTaillightBinding;
import bb.lanxing.devices.utils.XZDeviceHelper;
import bb.lanxing.lib.devices.api.ConnectionListener;
import bb.lanxing.lib.devices.api.DeviceManager;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.base.DeviceContext;
import bb.lanxing.lib.devices.core.utils.DeviceHelper;
import bb.lanxing.model.database.Device;
import bb.lanxing.activity.base.BaseViewActivity;
import bb.lanxing.view.BiciAlertDialogBuilder;

public class RadarTaillightActivity extends BaseViewActivity implements ConnectionListener {
    private String address;
    ActivityRadarTaillightBinding binding;
    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction() == null || !intent.getAction().equals("ACTION_BATTERY")) {
                return;
            }
            int intExtra = intent.getIntExtra("EXTRA_DEVICE_TYPE", 18);
            int intExtra2 = intent.getIntExtra("EXTRA_BATTERY", 0);
            String stringExtra = intent.getStringExtra("EXTRA_DEVICE_ADDRESS");
            if (intExtra != 18 || !stringExtra.equals(RadarTaillightActivity.this.address) || RadarTaillightActivity.this.radarSettingBean == null) {
                return;
            }
            RadarTaillightActivity.this.radarSettingBean.setBattery(intExtra2);
        }
    };
    private Device device;
    private DeviceManager dm;
    private RadarSettingBean radarSettingBean;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        ActivityRadarTaillightBinding activityRadarTaillightBinding =
                DataBindingUtil.setContentView(this, R.layout.activity_radar_taillight);
        this.binding = activityRadarTaillightBinding;
        setContentView(activityRadarTaillightBinding.getRoot());
        setupActionBar(true);
        setTitle(R.string.device_radar_title);
        String stringExtra = getIntent().getStringExtra("EXTRA_DEVICE_ADDRESS");
        this.address = stringExtra;
        Device byAddress = Device.getByAddress(stringExtra);
        this.device = byAddress;
        if (byAddress == null) {
            finish();
        }
        this.dm = DeviceContext.getDeviceManager();
        DeviceHelper.registerConnectionStateListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        this.radarSettingBean = RadarSettingBean.getInstance(this.address);
        if (XZDeviceHelper.isConnected(this.device.getAddress())) {
            setDeviceStatus(2);
        } else {
            DeviceManager deviceManager = this.dm;
            if (deviceManager != null) {
                deviceManager.connect(this.device);
            }
        }
        registerBroadcastReceiver();
    }

    private void registerBroadcastReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("ACTION_BATTERY");
        registerReceiver(this.broadcastReceiver, intentFilter);
    }

    @Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(this.broadcastReceiver);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_unbind, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem menuItem) {
        if (menuItem.getItemId() == R.id.menu_item_unbind) {
            new BiciAlertDialogBuilder(this)
                    .setTitle(R.string.xoss_g_unbind_dialog_title)
                    .setMessage(R.string.xoss_g_unbind_dialog_message)
                    .setPositiveButton(R.string.dialog_btn_ok, (dialogInterface, i) -> {
                        RadarTaillightActivity.this.unBindDevice();
                        DeviceContext.startAndRefreshAutoConnector();
                        RadarTaillightActivity.this.finish();
                    })
                    .setNegativeButton(R.string.dialog_btn_cancel, (DialogInterface.OnClickListener) null).show();
        }
        return super.onOptionsItemSelected(menuItem);
    }

    public void unBindDevice() {
        Device device = this.device;
        if (device != null) {
            device.delete();
        }
        App.getContext().disconnectDevice(18);
        finish();
    }

    private void setDeviceStatus(int i) {
        this.binding.tvConnectStatus.setOnClickListener(null);
        if (i == 1) {
            this.binding.tvConnectStatus.setCompoundDrawablesWithIntrinsicBounds(
                    ResourcesCompat.getDrawable(getResources(), R.drawable.ic_device_connecting, null),
                    (Drawable) null, (Drawable) null, (Drawable) null);
            this.binding.tvConnectStatus.setText(R.string.mine_device_label_connecting);
            this.binding.tvConnectStatus.setTextColor(getResources().getColor(R.color.blue_11aefa));
        } else if (i == 2) {
            this.binding.tvConnectStatus.setCompoundDrawablesWithIntrinsicBounds(
                    ResourcesCompat.getDrawable(getResources(), R.drawable.ic_device_connected, null),
                    (Drawable) null, (Drawable) null, (Drawable) null);
            this.binding.tvConnectStatus.setText(R.string.mine_device_label_connected);
            this.binding.tvConnectStatus.setTextColor(getResources().getColor(R.color.color_21d352));
        } else if (i == 4) {
            this.binding.tvConnectStatus.setCompoundDrawablesWithIntrinsicBounds(
                    ResourcesCompat.getDrawable(getResources(), R.drawable.ic_tv_connect_failed, null),
                    (Drawable) null, (Drawable) null, (Drawable) null);
            toast(R.string.mine_device_toast_connect_failed);
            this.binding.tvConnectStatus.setText(R.string.device_radar_click_to_connect);

            this.binding.tvConnectStatus.setOnClickListener(view -> {
                if (RadarTaillightActivity.this.dm != null) {
                    RadarTaillightActivity.this.dm.connect(RadarTaillightActivity.this.device);
                }
            });
        }
    }

    @Override
    public void onConnectionStateChanged(SmartDevice smartDevice, int i, int i2) {
        setDeviceStatus(i);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        DeviceHelper.unregisterConnectionStateListener(this);
        this.dm = null;
    }

    public static class RadarSettingBean extends BaseObservable {
        private int battery;

        private RadarSettingBean(int i) {
            battery = i;
        }

        public static RadarSettingBean getInstance(String str) {
            return new RadarSettingBean(XZDeviceHelper.getBattery(str));
        }

        @Bindable
        public int getBattery() {
            return battery;
        }

        public void setBattery(int i) {
            battery = i;
            notifyPropertyChanged(17);
        }
    }
}